package com.hongxin.nw.netty.down;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.model.Transfer;
import com.hongxin.nw.netty.model.packing.ASDUcsdna1;
import com.hongxin.nw.netty.model.packing.info.Csdna1_17;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.utils.pools.AsduStartPools;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;

/**
 * @remark 充电设备上报参数设置
 * @author: GuoXiaolin
 * @date 2019/6/25 15:59
 * @Version 1.0
 * @Descibe
 */
public class ParamSetUpReq implements IChannelRequest {
    @Override
    public String doPacking(Transfer transfer) throws Exception {

        String jsonStr = String.valueOf(transfer.getObj());
        String code = transfer.getCode();

        JSONObject jsonObject = JSONObject.parseObject(jsonStr);

        Csdna1_17 csdna1_17 = new Csdna1_17()
                .setCo(code)//设备编号
                .setChargeIndex(jsonObject.getString("bj"))//枪口号
                .setMonitorInterval(jsonObject.getString("monitorInterval"))//实时监测数据上报时间间隔,时间单位：分 ,0表示不上传
                .setChangeInterval(jsonObject.getString("changeInterval"));//变化数据上传最小时间间隔,时间单位：秒 ,0表示不上传

        //TYPE = 133 , COT = 45
        ASDUcsdna1 mm = new ASDUcsdna1();
        mm.setLogType(String.valueOf(TypeIdentif.RECORD_SET_PARAM.getInt()));
        mm.setSendData(csdna1_17.packing());

//
//        as.setType(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt());
//        as.setCot(TypeIdentif.COT_UNKNOWN_COT.getInt());
//        as.setStaadr(AsduStartPools.getAsduStart(code).getStatAddress());
//        as.setInfoadr(jsonObject.getIntValue("bj"));
//        as.setVsq(StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String as = FieldPack.packASDU(TypeIdentif.IDENTIFY_C_SD_NA_1.getInt(),
                TypeIdentif.COT_UNKNOWN_COT.getInt(),
                AsduStartPools.getAsduStart(code).getStatAddress(), jsonObject.getIntValue("bj"),
                StaticValue.DEFAULT_VSQ, StaticValue.SQ0);

        String data = as + mm.getHexData();

//        APCI ac = new APCI();
//        ac.setIdentifyStart(TypeIdentif.IDENTIFY_START.getInt());
//        ac.setApduLength(as.getHexData().length()/2);
//        ac.setContarea(TypeIdentif.FORMAT_I.getStr(), StaticValue.U_TEST_V);
//        return ac.getHexData() + as.getHexData();

        String ac = FieldPack.packAPCI(data.length(), TypeIdentif.FORMAT_I.getStr(), StaticValue.U_TEST_V);
        return ac + data;
    }
}
